package cc.orange.adapter;

import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import cc.orange.R;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.ConveListEntity;
import cc.orange.mainView.ShellLikeActivity;
import cc.orange.utils.CircleImageView;
import cc.orange.utils.ScreenUtils;
import cc.orange.utils.emojis.AndroidEmoji;
import demo.smart.access.xutlis.util.ZXAppUtil;
import demo.smart.access.xutlis.util.ZXScreenUtil;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import demo.smart.access.xutlis.util.ZXSystemUtil;


/**
 * Created by CM on 2022/10/25.
 */

public class MsgConAdapter extends BaseQuickAdapter<ConveListEntity.Data.Lists, BaseViewHolder> {
    ZXSharedPrefUtil sharedPrefUtil;//自己的头像
    public MsgConAdapter(@Nullable List<ConveListEntity.Data.Lists> data) {
        super(R.layout.rc_message_item, data);
        sharedPrefUtil = new ZXSharedPrefUtil();
    }

    private String name,photo,uid;//对方的 姓名 头像 id
    public void setNamePhoto(String name,String photo,String uid){
        this.name = name;
        this.photo = photo;
        this.uid = uid;
    }
    @Override
    protected void convert(@NonNull BaseViewHolder helper, ConveListEntity.Data.Lists item) {
        CircleImageView rc_left_portrait = helper.getView(R.id.rc_left_portrait);
        CircleImageView rc_right_portrait = helper.getView(R.id.rc_right_portrait);
        CircleImageView talks_msgs_img1 = helper.getView(R.id.talks_msgs_img1);//图片

        ConstraintLayout rc_cl_content = helper.getView(R.id.rc_cl_content);
        RelativeLayout rc_content = helper.getView(R.id.rc_content);//
        RelativeLayout item_congv_rel2 = helper.getView(R.id.item_congv_rel2);//布局2 提示文字
        RelativeLayout item_congv_rel3 = helper.getView(R.id.item_congv_rel3);//布局3 带头像提示文字
        if (item.getId() == -112) {
            item_congv_rel2.setVisibility(View.VISIBLE);
            rc_cl_content.setVisibility(View.GONE);
        } else {
//        if(types == 3){
//            item_congv_rel3.setVisibility(View.VISIBLE);
//            item_congv_rel2.setVisibility(View.GONE);
//            rc_cl_content.setVisibility(View.GONE);
//        }else{
            item_congv_rel2.setVisibility(View.GONE);
            rc_cl_content.setVisibility(View.VISIBLE);
//            item_congv_rel3.setVisibility(View.GONE);
//            item_congv_rel2.setVisibility(View.GONE);
//        }
            TextView talk_msg_content = helper.getView(R.id.talk_msg_content); //消息文本
            TextView rc_time = helper.getView(R.id.rc_time); //时间

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0, 20, 0, 0);
            talk_msg_content.setText("");

            if (item.getClassify() == 0) {
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                rc_content.setBackgroundResource(R.drawable.shape_msg_left);
                talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.color_ffd800));
                rc_left_portrait.setVisibility(View.VISIBLE);
                rc_right_portrait.setVisibility(View.GONE);
                if(!TextUtils.isEmpty(photo)) {
                    RequestOptions qe = new RequestOptions();
                    qe.error(photo.equals("img_1") ?
                            R.drawable.icon_login_img3 : R.drawable.icon_login_img4);
                    Glide.with(mContext).load(photo).apply(qe).into(rc_left_portrait);
                }else{
                    Glide.with(mContext).load(R.drawable.icon_login_img3).into(rc_left_portrait);
                }
            } else {
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                rc_content.setBackgroundResource(R.drawable.shape_msg_right);
                talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.white));
                rc_left_portrait.setVisibility(View.GONE);
                rc_right_portrait.setVisibility(View.VISIBLE);
                String img_url = sharedPrefUtil.getString("url_hr", "");
                RequestOptions qe = new RequestOptions();
                if(!TextUtils.isEmpty(img_url)) {
                    qe.error(img_url.equals("img_1") ?
                            R.drawable.icon_login_img3 : R.drawable.icon_login_img4);
                }else{
                    qe.error(R.drawable.icon_login_img3);
                }
                Glide.with(mContext).load(img_url).apply(qe).into(rc_right_portrait);
            }
            rc_content.setLayoutParams(layoutParams);
//
//
            if (!TextUtils.isEmpty(item.getInformationType()) && item.getInformationType().equals("1")) {//消息类型(0文本,1图片)
                talk_msg_content.setVisibility(View.GONE);
                Glide.with(mContext).load(item.getChattingRecords()).into(talks_msgs_img1);
                talks_msgs_img1.setVisibility(View.VISIBLE);

                int i = ZXScreenUtil.getScreenWidth() / 3;
                RelativeLayout.LayoutParams pp = new RelativeLayout.
                        LayoutParams(i, (int) (i * 1.5));
                talks_msgs_img1.setLayoutParams(pp);
            } else {
                talk_msg_content.setVisibility(View.VISIBLE);
                talks_msgs_img1.setImageResource(0);
                talks_msgs_img1.setVisibility(View.GONE);
                String content = item.getChattingRecords();
                if(!TextUtils.isEmpty(content)) {
                    content = content.replace("\n", " ");
                    if (content.length() > 100) {
                        content = content.substring(0, 100);
                    }
                    talk_msg_content.setText(new SpannableString(AndroidEmoji.ensure(content)));
                }
            }
        }
    }
}
